package com.group3.merchant.servlet;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.group3.entity.Dish;
import com.group3.merchant.service.IDishService;
import com.group3.merchant.service.impl.DishServiceImpl;
import com.group3.merchant.utils.SenderUtil;

/**
 * Servlet implementation class AddDishServlet
 */
public class AddDishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IDishService service = new DishServiceImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddDishServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("AddDishServlet");
		Dish dish=addDish(request, response);
		
		System.out.println("Add Dish: " + dish);
		//转发重定向
		if (dish!=null) {
//			String jsonStr = JsonUtil.getInstance().ObjectToJson(dish);
//			PrintWriter writer = response.getWriter();
//			writer.write(jsonStr);
//			List<Dish> dishes = service.showByMId(dish.getmId());
//			Map<String, Object> map = new HashMap<String, Object>();
//			map.put("dishes", dishes);
//			SenderUtil.send(map, response);
			response.sendRedirect("mainpage.html");
		} 
		else {
			System.out.println("add fail");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("AddDishServlet");
		Dish dish=addDish(request, response);
		System.out.println("Add Dish: " + dish);
		if (dish!=null) {
//			List<Dish> dishes = service.showByMId(dish.getmId());
//			Map<String, Object> map = new HashMap<String, Object>();
//			map.put("dishes", dishes);
//			SenderUtil.send(map, response);
			response.sendRedirect("mainpage.html");
		}
		else {
			System.out.println("add fail");
		}
	}

	private Dish addDish(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO Auto-generated method stub
		Dish tmp=null;
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if (isMultipart) {
			DiskFileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			String filePath = "";
			Dish dish = new Dish();
			try {
				List<FileItem> list = upload.parseRequest(request);
				Iterator<FileItem> i = list.iterator();
				while (i.hasNext()) {
					FileItem item = i.next();
					if (item.isFormField()) {
						switch (item.getFieldName()) {
						case "dName":
							System.out.println(item.getString("utf-8"));
							dish.setdName(item.getString("utf-8"));
							break;
						case "dPrice":
							dish.setdPrice(Double.valueOf(item
									.getString("utf-8")));
							
							break;
						case "pId":
							dish.setpId(item.getString("utf-8"));
							request.getSession().setAttribute("dType", item.getString("utf-8"));
							break;
						case "mid":
							System.out.println("mid"+item.getString("utf-8"));
							dish.setmId(item.getString("utf-8"));
							break;
						
						default:
							break;
						}
					} else {
						switch (item.getFieldName()) {
						case "image":
							File imageFile = new File("d://AMC/Dish/"
									+ UUID.randomUUID() + item.getName());
							filePath = imageFile.getAbsolutePath();
							dish.setdImageURL(filePath);
							item.write(imageFile);
							break;
						default:
							break;
						}
					}
				}
				// 调service
				System.out.println("Dish: " + dish);
			 tmp=service.addDish(dish);

//				System.out.println(dish);
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return tmp;
	}

}
